<?php
// +----------------------------------------------------------------------
// | ThinkPHP [ WE CAN DO IT JUST THINK ]
// +----------------------------------------------------------------------
// | Copyright (c) 2006-2016 http://thinkphp.cn All rights reserved.
// +----------------------------------------------------------------------
// | Licensed ( http://www.apache.org/licenses/LICENSE-2.0 )
// +----------------------------------------------------------------------
// | Author: 流年 <liu21st@gmail.com>
// +----------------------------------------------------------------------
use PHPMailer\PHPMailer\PHPMailer;
use think\Loader;
// 应用公共文件
function qrcode($content = '',$size = 4, $margin=2, $errorLevel = 'L'){
	Loader::import("phpqrcode",EXTEND_PATH);
    QRcode::png($content, false, $errorLevel, $size, $margin);
}

/**
 * @desc 获取链接最后的名称
 * @param string $url
 */
function getCurlFunc($url){
    $strs = explode('/', $url);
    if($strs){
        $last = $strs[count($strs)-1];
        return explode('.', $last)[0];
    }
    return null;
}

/**
 * 判断是否是微信浏览器
 * @return boolean
 */
function is_weixin() {
    if (strpos($_SERVER['HTTP_USER_AGENT'], 'MicroMessenger') !== false) {
        return true;
    }
    return false;
}

/**
 * 
 * @param string $image
 * @param string $suffix 后缀,静态使用,200列表图,750x400详情图,750详情文件
 * @return string
 */
function get_image($image,$suffix=''){
    
    if(empty($image)){
        return get_default_img();
    }
    if(strpos($image, 'http', 0) === 0){
        return $image;
    }
    if(ENVIRONMENT == 'production'){
        return IMG_DOMAIN.$image.$suffix;
    }else{
        return $image;
    }
}

function get_default_img(){
    return '/static/ace/assets/images/avatars/avatar5.png';
}

function getPwd($tel){
    $password=substr($tel,-6);
    return md5($password);
}

function readExcel($file) {
    $reader = PHPExcel_IOFactory::createReader('Excel2007');
    $objPHPExcel = $reader->load($file);
    return $objPHPExcel;
}

function exportExcel($objWriter, $printname) {
//     $objWriter = \PHPExcel_IOFactory::createWriter($excel, 'Excel2007');
    ob_end_clean();//清除缓冲区,避免乱码,LZ,2015-12-21
    header("Content-Type: application/vnd.ms-excel;");
    header("Content-Disposition:attachment;filename=".urlencode($printname));
    $objWriter->save('php://output');
    exit;
}

/**
 * 系统邮件发送函数
 * @param string $tomail 接收邮件者邮箱
 * @param string $name 接收邮件者名称
 * @param string $subject 邮件主题
 * @param string $body 邮件内容
 * @param string $attachment 附件列表
 * @return boolean
 * @author static7 <static7@qq.com>
 */
function send_mail($tomail, $name, $subject = '', $body = '', $attachment = null) {
    $mail = new PHPMailer();           //实例化PHPMailer对象
    $mail->CharSet = 'UTF-8';           //设定邮件编码，默认ISO-8859-1，如果发中文此项必须设置，否则乱码
    $mail->IsSMTP();                    // 设定使用SMTP服务
    $mail->SMTPDebug = 0;               // SMTP调试功能 0=关闭 1 = 错误和消息 2 = 消息
    $mail->SMTPAuth = true;             // 启用 SMTP 验证功能
    $mail->SMTPSecure = 'ssl';          // 使用安全协议
    $mail->Host = "smtp.exmail.qq.com"; // SMTP 服务器
    $mail->Port = 465;                  // SMTP服务器的端口号
    $mail->Username = "autoreply@mlilife.com";    // SMTP服务器用户名
    $mail->Password = "Wx_123456";     // SMTP服务器密码
    $mail->SetFrom('autoreply@mlilife.com', '品商');
    $replyEmail = '';                   //留空则为发件人EMAIL
    $replyName = '';                    //回复名称（留空则为发件人名称）
    $mail->AddReplyTo($replyEmail, $replyName);
    $mail->Subject = $subject;
    $mail->MsgHTML($body);
    $mail->AddAddress($tomail, $name);
    if (is_array($attachment)) { // 添加附件
        foreach ($attachment as $file) {
            is_file( $file['path']) && $mail->AddAttachment($file['path'],$file['name']);
        }
    }
    
    return $mail->Send() ? true : $mail->ErrorInfo;
}

function getBetweenDays($startDate, $endDate) {
    $startDateTime = strtotime($startDate);
    $endDateTime = strtotime($endDate);
    return intval(round(($endDateTime - $startDateTime) / 3600 / 24));
}

/**
 * 配置域名
 * @return mixed|array|boolean|NULL
 */
function get_host() {
    return config('host');
}

function get_appid(){
    return config('appid');
}

/**
 * 替换article 文章
 * @param string $content
 * @return mixed
 */
function replace_article_images($content){
    if(empty($content)){
        return '';
    }
    libxml_use_internal_errors(true) ;
    $doc = new DOMDocument();
    $doc->loadHTML($content);
    $xpath = new DOMXPath($doc);
    $nodeList = $xpath->query("//img");
    foreach($nodeList as $node){
        $image = $node->attributes->getNamedItem('src')->nodeValue;
        $content = str_replace($image, get_image($image,'!750'), $content);
    }
    return $content;
}
